package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Voice;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

@DubboService
public class VoiceApiImpl implements VoiceApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //保存音频文件
    @Override
    public String save(Voice voice) {
        voice.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(voice);
        //3、返回对象id
        return voice.getId().toHexString();
    }

    //获取音频文件
    @Override
    public Voice getVideo(Long userId) {
        //创建条件构造器
        Query query = Query.query(Criteria.where("userId").nin(userId).and("flag").is(false));
        Update update = new Update();
        update.set("flag", true);
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        Voice voice = mongoTemplate.findAndModify(query, update, options, Voice.class);
        return voice;
    }

}
